package com.csy.Controller;


import com.csy.View.Alert;
import com.csy.View.*;
import com.csy.View.Dialog;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Vector;
import java.util.regex.Pattern;


import com.csy.mysql_util.MysqlUtil;
import com.csy.View.MainWindow;

import javax.swing.*;

/**
 * 页面控制类 所有页面的控制 包含的方法  exec() 和 $()
 * 包含的属性为View包下的页面类
 */
public class Controller {
    private Dialog dialog;
    private Register register;
    private  MainWindow mainWindow;
    public static Alert alert;

    public Controller() throws IOException {
        dialog = new Dialog();
        register = new Register();
        dialog.show();
    }

    /**
     * 该方法实现各个页面的各种交互 例如点击当前页面的按钮 跳转至另一个页面 所有功能集合
     *
     * @throws ClassNotFoundException
     */
    public void exec() throws ClassNotFoundException {

        initEvent();
        dialogExec();
        registerExec();
//        sendMsgExec();
        dialog.show();
    }

    /**
     * 初始化事件
     */
    public void initEvent() {
        ((Button) $(dialog, "register")).setOnAction(event -> {
            dialog.hide();
            dialog.clear();
            register.show();
        });
        ((Button) $(register, "back")).setOnAction(event -> {
            register.hide();
            register.clear();
            dialog.show();
        });
    }

    private Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }

    private void dialogExec() {
        ((Button) $(dialog, "enter")).setOnAction(event -> {
            dialog.resetErrorTip();
            String UserName = ((TextField) $(dialog, "UserName")).getText();
            String Password = ((PasswordField) $(dialog, "Password")).getText();
            if (UserName.equals("") || Password.equals("")) {
                if (UserName.equals("")) {
                    dialog.setErrorTip("accountError", "！未输入账号");
                }
                if (Password.equals("")) {
                    dialog.setErrorTip("passwordError", "！未输入密码");
                }
            } else {
                MysqlUtil user=new MysqlUtil("",UserName,Password);
                int temp;
                {
                    try
                    {
                        StringBuffer name = new StringBuffer("");
                        temp = user.checkAccout(name);
                        String Ip=user.getIp();
                        if(temp==1)
                        {
                            dialog.close();
//                            new login_success(name,Ip);
                            mainWindow = new MainWindow(name,Ip);
                            mainWindow.show();
                        }
                        else{
                            dialog.setErrorTip("accountError", "！账号或密码错误");
                        }
                    }
                    catch (ClassNotFoundException ex)
                    {
                        ex.printStackTrace();
                    }
                    catch (SQLException ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }
        });
    }

    public void registerExec() {
        ((Button) $(register, "register")).setOnAction(event -> {
            register.resetErrorTip();
            String account = ((TextField) $(register, "account")).getText();
            String name = ((TextField) $(register, "name")).getText();
            String password = ((PasswordField) $(register, "password")).getText();
            String rePassword = ((PasswordField) $(register, "rePassword")).getText();
            String accountRegExp = "^[0-9,a-z,A-Z,\\u4e00-\\u9fa5]{1,15}$";
            String nameRegExp = "^[0-9,a-z,A-Z,\\u4e00-\\u9fa5]{1,100}$";
            String passwordReExp = "^[a-z,A-Z,0-9]{6,20}$";
            String rePasswordRegExp = "^[a-z,A-Z,0-9]{6,20}$";
            if (account.equals("") || name.equals("") || password.equals("") || rePassword.equals("")) {
                if (account.equals("")) {

                    register.setErrorTip("accountError", "！未输入账号");

                }
                if (name.equals("")) {
                    register.setErrorTip("nameError", "！未输入姓名");
                }
                if (password.equals("")) {
                    register.setErrorTip("passwordError", "！未输入密码");
                }
                if (rePassword.equals("")) {
                    register.setErrorTip("rePasswordError", "！未输入密码");
                }
            } else if (!Pattern.matches(accountRegExp, account) || !Pattern.matches(nameRegExp, name) ||
                    !Pattern.matches(passwordReExp, password) || !Pattern.matches(rePasswordRegExp, rePassword)) {

                if (!Pattern.matches(accountRegExp, account)) {
                    register.setErrorTip("accountError", "！错误,账号是长度不超过15位的中文和英文和数字");
                }
                if (!Pattern.matches(nameRegExp, name)) {
                    register.setErrorTip("nameError", "！姓名格式错误");
                }
                if (!Pattern.matches(passwordReExp, password)) {
                    register.setErrorTip("passwordError", "！错误,密码是长度在6-20位的英文字母和数字");
                }
                if (!Pattern.matches(rePasswordRegExp, rePassword)) {
                    register.setErrorTip("rePasswordError", "！错误,密码是长度在6-20位的英文字母和数字");
                }

            } else {
                if (password.equals(rePassword)) {
                    MysqlUtil user = new MysqlUtil(name, account, password);
                    boolean temp;
                    {
                        try {
                            int tempInt = user.CreatAccount();
                            if(tempInt==0)
                            {
                                register.close();
                                register.clear();
                                dialog.show();
                                dialog.clear();
                            }
                            if (tempInt == 1)
                            {
                                register.setErrorTip("accountError", "！注册失败,账号已存在");
                            }
                        } catch (ClassNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }
                } else {
                    register.setErrorTip("rePasswordError", "！两次密码不一致");
                }
            }
        });
    }
}